require(['init'], function () {
    //获取当前的id
    var type = location.search.urlToObj().type
    var classeid = location.search.urlToObj().classeid;
    var classe = location.search.urlToObj().classe;
    var userid = "", currentid = "";
    $.ajax({
        url: ' /api/user/current',
        type: 'get',
        async: false,
        success: function (data) {
            userid = data.data.userid;
          }
    });
    $.ajax({
        url: '/api/teacher/user/' + userid,
        type: 'get',
        async: false,
        success: function (json) {
            currentid = json.data.teacheruid //当前老师的id
        }
    })
    function getstudentjob() {
        $.ajax({
            url: '/api/task/' + currentid + '/schedules',
            type: 'get',
            data: {
                type: type,
                classeid: classeid
            },
            success: function (data) {
                var $listTable = $('#teacher_job').html('');
                $thead = $('<thead>\
                <tr>\
                <th >' + i18next.t('table.no') + '</th>\
                <th >' + i18next.t('table.lessonsname') + '</th>\
                <th >' + i18next.t('table.schooltime') + '</th>\
                <th >' + i18next.t('table.submitnumberstudents') + '</th>\
                <th >' + i18next.t('table.handle') + '</th>\
                </tr>\
              </thead>').appendTo($listTable)
                var $tbody = $('<tbody></tbody>').appendTo($listTable)
                $.each(data.data, function (index, item) {
                    $('<tr></tr>').appendTo($tbody);
                    var $tr = $('<tr></tr>').appendTo($tbody);
                    $tr.append('<td>' + (index + 1) + '</td>');
                    $('<td>' + item.schedulename + '</td>').appendTo($tr);
                    $tr.append('<td>' + item.begintime + '</td>');
                    $tr.append('<td>' + (item.replys) + '/' + (item.students) + '</td>');
                    var $operation = $('<td></td>').appendTo($tr);
                    if (item.status == 1) {
                        $('<a class="btn btn-primary btn-sm btn-inverse ml5" href="job.html?classeid=' + classeid + '&type=' + type + '&teacheruid=' + currentid + '&scheduleid=' + item.scheduleid + '&classe=' + classe + '">' + i18next.t('table.createjob') + '</a>').appendTo($operation);
                    } else if (item.status == 2) {
                        //发布
                        var $release = $('<a class="btn btn-primary btn-sm btn-inverse ml5" style="">'+i18next.t('table.issue')+'</a>').appendTo($operation);
                         //编辑
                        var $edit = $('<a class="btn btn-primary btn-sm btn-inverse ml5" href="job.html?classeid=' + classeid + '&type=' + type + '&teacheruid=' + currentid + '&scheduleid=' + item.scheduleid + '&classe=' + classe + '&taskid='+item.taskid+'">' +i18next.t('table.edit')+ '</a>').appendTo($operation);
                        $release.on('click', function () {
                          $.ajax({
                              url: '/api/task/subject/' + item.taskid + '/publish',
                              type: 'put',
                              success: function (data) {
                                  location.reload();
                              }
                          })
                        })
                    } else {
                       //批改作业
                        $('<a class="btn btn-primary btn-sm btn-inverse ml5" href="jobs_list.html?taskid=' + item.taskid + '&classe=' + classe + '&type='+type+'&classeid='+classeid+'">' + i18next.t('table.homeworkcorrecting') + '</a>').appendTo($operation)
                        //查看
                      $('<a class="btn btn-primary btn-sm btn-inverse ml5" href="job_look.html?classeid=' + classeid + '&type=' + type + '&teacheruid=' + currentid + '&scheduleid=' + item.scheduleid + '&classe=' + classe + '&taskid='+item.taskid+'&type='+type+'&classeid='+classeid+'">'+i18next.t('table.look')+'</a>').appendTo($operation);
                    }
                })
            }
        })
    }

    getstudentjob();
})
